﻿@{
    Layout = null;
}
<script src="../../Scripts/jquery-1.8.2.min.js" type="text/javascript"></script>
 <script src="../../Scripts/jquery.unobtrusive-ajax.min.js" type="text/javascript"></script>
<link rel="stylesheet" type="text/css" href="../../Content/Styles/Adminpage/reset.css">
<link rel="stylesheet" type="text/css" href="../../Content/Styles/Adminpage/structure.css">
   <div class="box login detailcontainer" id="form">
            <fieldset class="boxBody">
                <label>Username</label>
                <input type="text" tabindex="1"  required id="UsernameLogin">
                <label>Password</label>
               <input type="password" tabindex="2" required  id="PasswordLogin">
            </fieldset>
       <footer>
           <input type="submit" class="btnLogin" value="Login" tabindex="4" id="Loginbtn" style="margin-left: 50px"/>
           <input type="reset" class="btnLogin" value="Reset" tabindex="4" id="ResetBtn"/>
           <div id="errorMsg" style="color: green"></div>
           @Html.Hidden("RedirectTo", Url.Action("AdminPage", "Admin"))
       </footer>
   </div>
        <script type="text/javascript">
            $(document).ready(function () {
                document.getElementById('UsernameLogin').focus();
                $('#Loginbtn').click(function () {
                    var username = $('input#UsernameLogin').val();
                    //var username = usernametemp.trim();
                    var password = $('input#PasswordLogin').val();
                    if (username == "" || password == "") {
                        $("div#errorMsg").css("display", "block");
                        $("div#errorMsg").html("Please input username and password");
                    } else { //call ajax function here
                        $.ajax({
                            url: "/Admin/CheckLogin",
                            type: 'POST',
                            data: { usernameinput: username, password: password },
                            success: function (response) {
                               if (response == "Fail") {
                                    $("div#errorMsg").css("display", "block");
                                    $("div#errorMsg").html("Sorry,Username or password is not correct");

                                }
                                else if(response == "Admin"){
                                    var url = $("#RedirectTo").val();
                                    location.href = url;


                                }
                            }
                        });


                    }
                });

                $(document).keydown(function (e) {
                    // ESCAPE key pressed
                    if (e.keyCode == 13) {
                        var username = $('input#UsernameLogin').val();
                        var password = $('input#PasswordLogin').val();
                        if (username == "" || password == "") {
                            $("div#errorMsg").css("display", "block");
                            $("div#errorMsg").html("Please Enter Username, Password!");
                        } else {//call ajax function here
                            $.ajax({
                                url: "/Admin/CheckLogin",
                                type: 'POST',
                                data: { usernameinput: username, password: password },
                                success: function (response) {
                                    //alert(response);
                                    if (response == "Fail") {
                                        $("div#errorMsg").css("display", "block");
                                        $("div#errorMsg").html("Sorry,Username or password is not correct");

                                    }
                                    else if (response == "Admin") {
                                        var url = $("#RedirectTo").val();
                                        location.href = url;


                                    }
                                }

                            });


                        }


                    }
                });

                $('#ResetBtn').click(function () {
                    document.getElementById('UsernameLogin').value = '';
                    document.getElementById('PasswordLogin').value = '';
                });

            });
            
        </script>